package com.ita.osc.listener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import com.ita.osc.bean.Cart;
import com.ita.osc.bean.Dish;
import com.ita.osc.bean.User;
import com.ita.osc.dao.CartDao;
import com.ita.osc.daoImpl.CartDaoImpl;
import com.ita.osc.service.CartManager;
import com.ita.osc.serviceImpl.CartManagerImpl;

/**
 * Application Lifecycle Listener implementation class CartSessionListener
 *
 */
public class CartSessionListener implements HttpSessionAttributeListener {
	
	private static final String CART_LISTENER_NAME = "cart";
	private static final String USER_LISTENER_NAME = "user";
	private CartManager cartManager = new CartManagerImpl();
	private CartDao cartDao = new CartDaoImpl();

    /**
     * Default constructor. 
     */
    public CartSessionListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
     */
    public void attributeAdded(HttpSessionBindingEvent arg0)  { 
         // TODO Auto-generated method stub
    	if(USER_LISTENER_NAME.equals(arg0.getName())){
    		HttpSession session = arg0.getSession();
    		User user = (User) arg0.getValue();
    		Cart cart = cartManager.getCartByUserId(user.getUid());
    		session.setAttribute("cart", cart);
    	}
    }

	/**
     * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
     */
    public void attributeRemoved(HttpSessionBindingEvent arg0)  { 
         // TODO Auto-generated method stub
    }
 
	/**
     * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
     */
    public void attributeReplaced(HttpSessionBindingEvent arg0)  { 
         // TODO Auto-generated method stub
    	
    	new Thread(new Runnable() {
			public void run() {
				if(CART_LISTENER_NAME.equals(arg0.getName())){
		    		String userId = ((Cart) arg0.getValue()).getUid();
		    		Map<Dish, Integer> afterCart = new HashMap<Dish, Integer>();
		    		afterCart.putAll(((Cart) arg0.getValue()).getCartList());
		    		Map<Dish, Integer> beforeCart = new HashMap<Dish, Integer>();
		    		beforeCart.putAll(cartManager.getCartByUserId(userId).getCartList());
		    		List<Dish> removeList = new ArrayList<Dish>();
		    		for (Dish dish : afterCart.keySet()) {
						if(beforeCart.containsKey(dish) && (beforeCart.get(dish) == afterCart.get(dish))){
							removeList.add(dish);
						}
					}
		    		for (Dish dish : removeList) {
						beforeCart.remove(dish);
						afterCart.remove(dish);
					}
		    		for (Dish dish : beforeCart.keySet()) {
						cartDao.removeDishFromCart(userId, dish.getDid());
					}
		    		for (Dish dish : afterCart.keySet()) {
						cartDao.addDishIntoCart(userId, dish.getDid(), afterCart.get(dish));
					}
		    	}
			}
		}).start();
    }
	
}
